﻿namespace ManagerClinic.DanhMuc
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using DevExpress.XtraEditors;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Ultilities.Ultility;
    using ManagerClinic.Modules;

    public partial class frmCapNhatDmHangSx : DevExpress.XtraEditors.XtraForm
    {
        DmHangSanXuatBusiness dmHangSanXuatBusiness = new DmHangSanXuatBusiness();
        DmQuocGiaBusiness dmQuocGiaBusiness = new DmQuocGiaBusiness();
        ThietLapMaBusiness thietLapMaBusiness = new ThietLapMaBusiness();

        DmHangSanXuatEntity dmHangSanXuatEntity = null;
        public event EventHandler dmHangSxEvent = null;

        int State = 0;

        public frmCapNhatDmHangSx(int state)
        {
            InitializeComponent();
            this.State = state;
            LoadQuocGia();
        }

        public frmCapNhatDmHangSx(int state, DmHangSanXuatEntity obj)
        {
            InitializeComponent();

            this.State = state;
            LoadQuocGia();
            dmHangSanXuatEntity = obj;

            txtMaHangSanXuat.Text = dmHangSanXuatEntity.MaHang;
            txtTenHangSanXuat.Text = dmHangSanXuatEntity.TenHang;
            chkTrangThaiSuDung.Checked = dmHangSanXuatEntity.TrangThaiSuDung;
            txtDiaChi.Text = dmHangSanXuatEntity.DiaChi;
            txtDienThoai.Text = dmHangSanXuatEntity.DienThoai;
            lkQuocGia.EditValue = dmHangSanXuatEntity.MaQuocGia;
            txtFax.Text = dmHangSanXuatEntity.Fax;
            txtEmail.Text = dmHangSanXuatEntity.Email;
            txtWebsite.Text = dmHangSanXuatEntity.Website;
            txtMoTa.Text = dmHangSanXuatEntity.MoTa;
            chkTrangThaiSuDung.Checked = dmHangSanXuatEntity.TrangThaiSuDung;
            txtTenHangSanXuat.Focus();
        }

        void LoadDmQuocGia(object sender, EventArgs e)
        {
            LoadQuocGia();
        }

        private void LoadQuocGia()
        {
            var lstDmQuocGiaEntity = dmQuocGiaBusiness.GetDmQuocGiaList();
            var dmQuocGiaEntity = new DmQuocGiaEntity()
            {
                MaQuocGia = ConstantManagerClinic.sValueMember,
                TenQuocGia = ConstantManagerClinic.sDisplayMember
            };
            lstDmQuocGiaEntity.Insert(0, dmQuocGiaEntity);

            lkQuocGia.Properties.DataSource = lstDmQuocGiaEntity;
            lkQuocGia.Properties.DisplayMember = "TenQuocGia";
            lkQuocGia.Properties.ValueMember = "MaQuocGia";
            lkQuocGia.EditValue = ConstantManagerClinic.sValueMember;
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (Validation.ValidLookUpEdit(lkQuocGia, "Quốc gia") == false) return;
            if (validateManager1.IsValid() == false) return;
            if (Validation.ValidSpaceTextBoxKey(txtMaHangSanXuat, "Mã hãng sản xuất") == false) return;
            if (Validation.ValidEmail(txtEmail, "Email hãng sản xuất") == false) return;

            dmHangSanXuatEntity = new DmHangSanXuatEntity()
            {
                MaHang = txtMaHangSanXuat.Text,
                TenHang = txtTenHangSanXuat.Text.Trim(),
                MaQuocGia = lkQuocGia.EditValue.ToString(),
                DiaChi = txtDiaChi.Text.Trim(),
                DienThoai = txtDienThoai.Text.Trim(),
                Fax = txtFax.Text.Trim(),
                Email = txtEmail.Text.Trim(),
                Website = txtWebsite.Text.Trim(),
                MoTa = txtMoTa.Text.Trim(),
                TrangThaiSuDung = (bool)chkTrangThaiSuDung.Checked
            };

            if (this.State == (int)ConstantManagerClinic.StateInsertOrUpdate.Insert)
            {
                if (dmHangSanXuatBusiness.GetDmHangSanXuat(txtMaHangSanXuat.Text.Trim()) != null)
                    if (Validation.ValidPrimaryKey(txtMaHangSanXuat, "Mã hãng sản xuất") == false) return;
                dmHangSanXuatBusiness.InsertDmHangSanXuat(dmHangSanXuatEntity);
                Validation.ClearControl(this);
                txtMaHangSanXuat.Focus();
            }
            else
            {
                dmHangSanXuatBusiness.EditDmHangSanXuat(dmHangSanXuatEntity);
                MessageBox.Show("Cập nhật thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            if (dmHangSxEvent != null)
                dmHangSxEvent(sender, e);
        }

        private void frmcapnhatdmhangsx_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                SendKeys.SendWait("{Tab}");
        }

        private void btnThemQG_Click(object sender, EventArgs e)
        {
            var frm = new frmCapNhatDmQuocGia((int)ConstantManagerClinic.StateInsertOrUpdate.Insert);
            frm.ButtonClicked += new EventHandler(LoadDmQuocGia);
            frm.ShowDialog();
        }
    }
}